//
//  LLLoadingHeaderView.m
//  PandaMakeUp
//
//  Created by dl on 16/1/12.
//  Copyright © 2016年 lrlz. All rights reserved.
//

#import "LLLoadingHeaderView.h"

@implementation LLLoadingHeaderView {
    LLUIImageView *titleImgView;
    LLUIImageView *loadingImgView;
}

- (instancetype)init {
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];

        titleImgView = [LLUIImageView new];
        [titleImgView setImage:[UIImage imageNamed:@"pull_down_title"]];
        [self addSubview:titleImgView];

        [titleImgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.bottom.mas_equalTo(-SUIT_H(74));
            make.width.mas_equalTo(SUIT_W(180));
            make.height.mas_equalTo(SUIT_H(20));
        }];

        loadingImgView = [LLUIImageView new];
        [loadingImgView setImage:[UIImage imageNamed:@"pull_down_loading"]];
        [self addSubview:loadingImgView];

        [loadingImgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.height.width.mas_equalTo(SUIT_H(20));
            make.bottom.mas_equalTo(SUIT_H(-28));
        }];
        [self circleLoadingView];
    }
    return self;
}

- (void)circleLoadingView {
    if ([loadingImgView.layer animationKeys].count == 0) {
        CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation.z"];
        animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
        animation.values = @[@(0), @(2 * M_PI)];
        animation.keyTimes = @[@0, @1];
        animation.duration = 0.8f;
        animation.repeatCount = INFINITY;             // 不使用这个，原因是app最小化，再打开，动画消失了
        [loadingImgView.layer addAnimation:animation forKey:@"loadingRoatation"];
    }

    @weakify(self)
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t) (0.8f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        @strongify(self)
        [self circleLoadingView];
    });
}

@end
